package com.ymatou.iapi.sellercs.testcase;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.CloseInactionSessionBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.CloseInactionSessionCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtCssession;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

import redis.clients.jedis.Tuple;
/**
*关闭不活跃的会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CloseInactionSession{
	private static CloseInactionSessionBean closeinactionsessionBean;
	private static CloseInactionSessionCall closeinactionsessionCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("关闭不活跃的会话");
	}

	@Before
	public void caseUp() {
		closeinactionsessionBean=new CloseInactionSessionBean();
		closeinactionsessionCall=new CloseInactionSessionCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(closeinactionsessionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseInactionSession_001() {
		Logger.start(true,"关闭不活跃的会话成功");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			Logger.debug(YmtSellerCSIMWapper.updateYmtCssessionLastMessageTime(cinfos.get(0).getId(), YMTDateUtil.getDateBeforeOrNextSecond(-60*60)));
			 cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			 Logger.debug("update："+FormatUtil.beanToGSONString(cinfos));
			closeinactionsessionBean.setAppId("autotest");
			closeinactionsessionCall.setData(closeinactionsessionBean);
			closeinactionsessionCall.callService();		
			Logger.verifyEquals("ok", closeinactionsessionCall.getReturnData(), "验证返回");
			Thread.sleep(5000);
			Logger.comment("验证mysql记录");
			cinfos=YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			Logger.debug("dojob："+FormatUtil.beanToGSONString(cinfos));
			if(cinfos.size()>=1){
				Logger.verifyEquals(1, cinfos.get(0).getStatus(), "验证status");
				Logger.verifyEquals(2, cinfos.get(0).getCloseType(), "验证closeType超时 2-TimeOut");
				Logger.verifyNotNull(cinfos.get(0).getEndTime(),"验证endtime");
			}
			
			Logger.comment("验证redis记录清除");
			ImRedisWapper ImRedisWapper=new ImRedisWapper();
			Set<Tuple> set = ImRedisWapper.getCSession(sendmessageBean.getToUserId());
			set.forEach(t->{Logger.debug("tuple:"+t.getScore()+":"+t.getElement());});
			List<String> list = set.stream().map(t->{return t.getElement();}).collect(Collectors.toList());
			Logger.verifyEquals(false,list.contains(sendmessageBean.getFromUserId().toString()),"验证CSession中包含有FromUserId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseInactionSession_002() {
		Logger.start(true,"最后消息时间未超过5分钟不关闭不活跃的会话");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
//			Logger.debug(YmtSellerCSIMWapper.updateYmtCssessionLastMessageTime(cinfos.get(0).getId(), YMTDateUtil.getDateBeforeOrNextSecond(-60*60)));
			 cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			 Logger.debug("update："+FormatUtil.beanToGSONString(cinfos));
			closeinactionsessionBean.setAppId("autotest");
			closeinactionsessionCall.setData(closeinactionsessionBean);
			closeinactionsessionCall.callService();		
			Logger.verifyEquals("ok", closeinactionsessionCall.getReturnData(), "验证返回");
			Thread.sleep(5000);
			Logger.comment("验证mysql记录");
			cinfos=YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			Logger.debug("dojob："+FormatUtil.beanToGSONString(cinfos));
			if(cinfos.size()>=1){
				Logger.verifyEquals(0, cinfos.get(0).getStatus(), "验证status");
				Logger.verifyIsNull(cinfos.get(0).getCloseType(), "验证closeType");
				Logger.verifyIsNull(cinfos.get(0).getEndTime(),"验证endtime");
			}
			
			Logger.comment("验证redis记录清除");
			ImRedisWapper ImRedisWapper=new ImRedisWapper();
			Set<Tuple> set = ImRedisWapper.getCSession(sendmessageBean.getToUserId());
			set.forEach(t->{Logger.debug("tuple:"+t.getScore()+":"+t.getElement());});
			List<String> list = set.stream().map(t->{return t.getElement();}).collect(Collectors.toList());
			Logger.verifyEquals(true,list.contains(sendmessageBean.getFromUserId().toString()),"验证CSession中包含有FromUserId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}